package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.user.SupplierAuthEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.AuthCenterActivity;
import com.sgb.kjwl.view.ui.activity.self.userInfo.VerificationCodeLoginActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.helper.AppManagerHelper;
import com.swgk.core.util.HttpErrorUtil;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import io.reactivex.functions.Consumer;

public class AuthCenterModel extends BaseViewModel {
    private AuthCenterActivity activity;
    private SelfRepertory repertory;

    public AuthCenterModel(AuthCenterActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getSupplierAuthModel() {
        repertory.getSupplierAuthInfo()
                .subscribe(new Consumer<BaseEntity<SupplierAuthEntity>>() {
                    @Override
                    public void accept(BaseEntity<SupplierAuthEntity> supplierAuthEntityBaseEntity) throws Exception {
                        activity.showSupplierAuthView(supplierAuthEntityBaseEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        MLog.d(AppConstant.API_LOG, throwable.getMessage());
                        if (HttpErrorUtil.getHttpErrorMsg(throwable).equals(HttpErrorUtil.HTTP_401)) {
                            MToast.showToast(activity, "token失效，请重新登录!");
                            AppManagerHelper.getAppManagerHelper().finishActivity(activity);
                            VerificationCodeLoginActivity.start(activity);
                        }
                    }
                });
    }
}
